package com.UI;
import com.Model.StudentDBModel;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AddStudyInfo extends Activity{

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.add_study_info);
		Bundle extras = getIntent().getExtras();
		final String cNo = extras.getString("CourseNum");
		
		// create a model instance
		final StudentDBModel newStudyInfo = new StudentDBModel(this);
		
		
		
		
		
		// register this activity as a button event listener
		Button btnStudyInfoSubmit = (Button) findViewById(R.id.btnStudyInfoSubmit);
		
		btnStudyInfoSubmit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				
		 		EditText txtGname = (EditText)findViewById(R.id.txtGroupName);
				String tempGname = txtGname.getText().toString().replaceAll(" ", "");
				
				if(tempGname.length() == 0) {
		  	      
		 		   AlertDialog alertDialog = new AlertDialog.Builder(AddStudyInfo.this).create();	        			
		 		   alertDialog.setTitle("Warning");	        		  
		 		   alertDialog.setMessage("Please Enter the Group Member's Name");	        		  	        		  
		 		   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
		 		  
		 		   public void onClick(DialogInterface dialog, int which) {
				       return;
				  } }); 
				  alertDialog.show();
				  
				}
				
				else {
				// Get the values from the user and send them to the DB
				ContentValues values = new ContentValues();
				values.put("CourseId", cNo);
		
				EditText txtGroupName = (EditText) findViewById(R.id.txtGroupName);
				values.put("GroupName",txtGroupName.getText().toString());
		
				EditText txtGroupTime = (EditText) findViewById(R.id.txtGroupTime);
				values.put("GroupTime", txtGroupTime.getText().toString());
		
				EditText txtGroupNotes= (EditText) findViewById(R.id.txtGroupNotes);
				if(txtGroupNotes.getText().toString().equals("")){
					values.put("GroupNotes","No Notes Entered");}
				else {
					values.put("GroupNotes", txtGroupNotes.getText().toString());}
		
				// use the model to store the data
				newStudyInfo.addNewStudyInfo(values);
				int numStudyInfo = newStudyInfo.getNumStudyInfo();
				// tell the user that study information was added to the database
				TextView lblResults = (TextView) findViewById(R.id.lblAddStudyInfoResult);
				lblResults.setText(numStudyInfo + " " + getString(R.string.StudyInfoAdded));
				
				//finish the activity to return to the calling event
				Intent viewStudyInfo = new Intent(AddStudyInfo.this, StudyInfoList.class);
				viewStudyInfo.putExtra("CourseNum", cNo);
				startActivity(viewStudyInfo);
				
				
				AddStudyInfo.this.finish();
				}
			}
		});
	
		//Home and Back Buttons
		Button btnGoBack = (Button) findViewById(R.id.btnBack);
		btnGoBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				// go back to the pick class event view
				AddStudyInfo.this.finish();
			}
		});
		
		Button btnGoHome = (Button) findViewById(R.id.btnHome);
		btnGoHome.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				// go to the home page
				Intent goHome = new Intent(AddStudyInfo.this, HomePage.class);
				startActivity(goHome);
				//finish the activity to return to the calling event
				AddStudyInfo.this.finish();
			}
		});
		
		
		
		
		
		
		
		
	}
}


